package net.yebaihe.wc.sina;

import weibo4android.WeiboException;
import weibo4android.http.AccessToken;
import weibo4android.http.RequestToken;
import net.yebaihe.wc.Index;
import net.yebaihe.wc.R;
import android.app.Activity;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class OAuthActivity extends Activity  
{  
  
    public void onCreate(Bundle savedInstanceState)  
    {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.sns_sina);  
        Uri uri = this.getIntent().getData();  
        try  
        {  
            RequestToken requestToken = OAuthConstant.getInstance().getRequestToken();  
            AccessToken accessToken = requestToken.getAccessToken(uri.getQueryParameter("oauth_verifier"));  
            OAuthConstant.getInstance().setAccessToken(accessToken);  
            
	        SharedPreferences settings = getSharedPreferences(Index.PREFS_NAME, 0);  
			SharedPreferences.Editor editor = settings.edit();  
			editor.putString("sinakey", accessToken.getToken());  
			editor.putString("sinasecret", accessToken.getTokenSecret());  
			editor.commit();
			
			Toast.makeText(this, "您的新浪微博帐号已经认证成功，现在可以同步到新浪微博了！", Toast.LENGTH_LONG).show();
			TextView txtHint=(TextView) OAuthActivity.this.findViewById(R.id.txtHint);
			txtHint.setText("账户已关联，按 返回键 返回主程序!");
			
        }  
        catch (WeiboException e)  
        {  
            e.printStackTrace();  
        }  
        /*Button button = (Button) findViewById(R.id.Button01);  
        button.setText("某一话题下的微博");  
        button.setOnClickListener(new Button.OnClickListener() {  
  
            public void onClick(View v)  
            {  
                Weibo weibo = OAuthConstant.getInstance().getWeibo();  
                weibo.setToken(OAuthConstant.getInstance().getToken(),  
                        OAuthConstant.getInstance().getTokenSecret());  
                List<Status> friendsTimeline;  
                try  
                {  
                    friendsTimeline = weibo.getTrendStatus("seaeast",  
                            new Paging(1, 20));  
                    StringBuilder stringBuilder = new StringBuilder("");  
                    for (Status status : friendsTimeline)  
                    {  
                        stringBuilder.append(status.getUser().getScreenName()  
                                + "说:\n"  
                                + status.getText()  
                                + "\n--------------------------------------------------\n");  
                    }  
                    TextView textView = (TextView) findViewById(R.id.TextView01);  
                    textView.setText(stringBuilder.toString());  
                }  
                catch (WeiboException e)  
                {  
                    e.printStackTrace();  
                }  
            }  
        }); */ 
    }  
}  
